package com.changgou.order.mq.listener;

import com.changgou.order.mq.queue.QueueConfig;
import com.changgou.order.pojo.Order;
import com.changgou.order.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@RabbitListener(queues = QueueConfig.QUEUE_MESSAGE)
public class DelayListener {

    @Autowired
    private OrderService orderService;

    /***
     * 监听消息
     * @param msg
     */
    @RabbitHandler
    public void msg(String msg){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("当前时间:"+dateFormat.format(new Date()));
        System.out.println("收到信息:"+msg);
        //根据收到的订单id查询订单状态
        Order order = orderService.findById(msg);
        String payStatus = order.getPayStatus();
        if (!"1".equals(payStatus)){
            //订单未支付
            //订单回滚(逻辑删除)
            orderService.deletOrder(msg);
        }
    }

}